package com.bsoft.exchange.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bsoft.exchange.dao.his.SystemConfigMapper;
import com.bsoft.exchange.dao.his.UserBillMapper;
import com.bsoft.exchange.domain.UserBillDomain;



/**
 * 员工票据类 门诊+住院，住院的函数为_ZY
 * @author yangxt
 */
@Component
public class UserBillManager {
	
	@Autowired
	IdentityManager identityManager;
	@Autowired
	SystemConfigMapper systemConfigMapper;
	
	@Autowired
	private  UserBillMapper userBillMapper;
	
	//mzzy  0门诊 1 住院  分别对应 MS_YGPJ  ZY_YGPJ
	/**门诊获取员工门诊号码票据
	 * @param jgid
	 * @param czgh
	 * @return
	 */
	public String getMzhm(Long jgid,String czgh){

		return getPjhmByPjlx(jgid,czgh,3,0);
	}
	/**门诊获取员工就诊号码票据
	 * @param jgid
	 * @param czgh
	 * @return
	 */
	public String getJzhm(Long jgid,String czgh){

		return getPjhmByPjlx(jgid,czgh,1,0);
	}
	
	/**更新门诊门诊号码票据
	 * @param jgid
	 * @param czgh
	 * @param dqhm
	 */
	public void updateMzhm(Long jgid,String czgh,String dqhm){
		updatePjhmByPjlx(jgid,czgh,dqhm,3,0);
	}
	/**更新门诊就诊号码票据
	 * @param jgid
	 * @param czgh
	 * @param dqhm
	 */
	public void updateJzhm(Long jgid,String czgh,String dqhm){
		updatePjhmByPjlx(jgid,czgh,dqhm,1,0);
	}
	
	/**住院获取员工结算发票号码票据
	 * @param jgid
	 * @param czgh
	 * @return
	 */
	public String getSjhm_ZY(Long jgid,String czgh){

		return getPjhmByPjlx(jgid,czgh,1,1);
	}
	/**更新住院结算发票号码票据
	 * @param jgid
	 * @param czgh
	 * @param dqhm
	 */
	public void updateSjhm_ZY(Long jgid,String czgh,String dqhm){
		updatePjhmByPjlx(jgid,czgh,dqhm,1,1);
	}
	
	public String getPjhmByPjlx(Long jgid,String czgh,Integer pjlx,Integer mzzy){
		
		String mzhm = "MZCS";
		//mzhm = mzhm + identityManager.getMax("MS_MZHM", 1);
		
		//默认工号
		if(czgh==null || "".equals(czgh)){
//			czgh = "1000025";   //100040  1000025
			String ygbh = systemConfigMapper.getValueByConfigName(1L, "ZSYY_MRGH");
			czgh = systemConfigMapper.getYgdmByYgbh(ygbh);
		}
//		czgh = "2000005";//暂时写死
		List<UserBillDomain> userBillList = new ArrayList<UserBillDomain>();
		if(mzzy ==0){
			userBillList = userBillMapper.getUserBillList(1L,czgh,pjlx);
		}else{
			userBillList = userBillMapper.getUserBillList_ZY(1L,czgh,pjlx);
		}

		//无票据
		if(userBillList == null || userBillList.size() <= 0){
			mzhm = "";
			return mzhm;
		}
		
		//LYRQ升序排序 取第一个
		UserBillDomain  userBill = userBillList.get(0);
		
		mzhm = userBill.getSyhm();
		
		
		return mzhm;
	}
	
	public void updatePjhmByPjlx(Long jgid,String czgh,String dqhm,Integer pjlx,Integer mzzy){
		
		//默认工号
		if(czgh==null || "".equals(czgh)){
			//czgh = "1000025"; //100040  1000025
			czgh = systemConfigMapper.getValueByConfigName(1L, "ZSYY_MRGH");
		}
		czgh = "2000005";
		List<UserBillDomain> userBillList = new ArrayList<UserBillDomain>();
		if(mzzy ==0){
			userBillList = userBillMapper.getUserBillList(1L,czgh,pjlx);
		}else{
			userBillList = userBillMapper.getUserBillList_ZY(1L,czgh,pjlx);
		}

		String zzhm;
		String syhm;
		int end ;
		Integer sypb = 0;
		String dqhmPrefix;
		Long dqhmHm;
		String dqhmHmS;
		Integer hmLen ;
		//票据
		if(userBillList != null && userBillList.size() > 0){
			//LYRQ升序排序 取第一个
			UserBillDomain  userBill = userBillList.get(0);
			zzhm = userBill.getZzhm();
			
			//当前号码+1
			end = -1;
			for(int i= dqhm.length() - 1;i>=0;i--){
				if(dqhm.charAt(i)>=48 && dqhm.charAt(i)<=57){
					  continue;
				  }else{
					  
					  end = i;
					  break;
				  }
			}
			
			dqhmPrefix = "";
			dqhmHmS = dqhm;
			
			if(end >= 0 ){
				dqhmPrefix = dqhm.substring(0,end + 1);
				dqhmHmS = dqhm.substring(end + 1);
			}

			syhm = dqhmPrefix + String.format("%0" + dqhmHmS.length() + "d", Long.parseLong(dqhmHmS) + 1);
			
			if(syhm.compareTo(zzhm) > 0){
				sypb = 1;
			}
			userBill.setJgid(1L);
			userBill.setYgdm(czgh);
			userBill.setSyhm(syhm);
			userBill.setSypb(sypb);
			userBill.setPjlx(pjlx);
			
			if(mzzy ==0){
				userBillMapper.updateUserBill(userBill);
			}else{
				userBillMapper.updateUserBill_ZY(userBill);
			}
			
		}

	}
	
}
